package com.xiaowen.codeData.controller;

import cn.hutool.core.util.ObjectUtil;
import com.xiaowen.codeData.dto.*;
import com.xiaowen.codeData.entity.CodeData;
import com.xiaowen.codeData.service.CodeDataService;
import com.xiaowen.organization.service.OrganizationService;
import com.xiaowen.tool.ObjUtils;
import com.xiaowen.tool.dto.Result;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.compress.utils.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yf-wenhao
 * @since 2024-10-02
 */
@RestController
@RequestMapping("/codeData")
@Slf4j
public class CodeDataController {

    @Autowired
    private CodeDataService codeDataService;
    @Autowired
    private OrganizationService organizationService;


    /**
     * 添加
     * @return the result
     * @author hero
     * @date 2024 -09-18 14:25:03
     */
    @PostMapping(value = "/add")
    public Result<Boolean> add(@RequestBody CodeData reqDTO) {
        if (ObjectUtil.isEmpty(reqDTO) || ObjectUtil.isEmpty(reqDTO.getProjectId())){
            return new Result<Boolean>().ok(false);
        }
        return new Result<Boolean>().ok(codeDataService.add(reqDTO));
    }

    /**
     * 更新
     * @return the result
     * @author hero
     * @date 2024 -09-18 14:25:03
     */
    @PostMapping(value = "/update")
    public Result<Boolean> update(@RequestBody CodeData codeData) {
        if (ObjectUtil.isEmpty(codeData) || ObjectUtil.isEmpty(codeData.getProjectId())){
            return new Result<Boolean>().ok(false);
        }
        codeData.setUpdateTime(new Date());
        return new Result<Boolean>().ok(codeDataService.update(codeData));
    }
    /**
     * 获取编码模板
     * @param projectId the id
     * @return the result
     * @author hero
     * @date 2024 -09-18 14:25:03
     */
    @GetMapping(value = "/list")
    public Result<List<CodeDataDTO>> listByProjectId(@RequestParam("projectId") Integer projectId, @RequestParam(value = "code", required = false) Integer code) {
        if (ObjectUtil.isEmpty(projectId)){
            return new Result<List<CodeDataDTO>>().ok(Lists.newArrayList());
        }
        return new Result<List<CodeDataDTO>>().ok(codeDataService.listByProjectId(projectId, code));
    }

    /**
     * 获取编码模板
     * @param projectId the id
     * @return the result
     * @author hero
     * @date 2024 -09-18 14:25:03
     */
    @GetMapping(value = "/generateLabel")
    public Result<List<OriginalCodeDTO>> generateLabel(@RequestParam("projectId") Integer projectId) {
        if (ObjectUtil.isEmpty(projectId)){
            return new Result<List<OriginalCodeDTO>>().ok(Lists.newArrayList());
        }
        List<OriginalCodeDTO> originalCodeDTOS = codeDataService.generateLabel(projectId);
        //判断
        Long surplusAmount = organizationService.getSurplusAmount();
        int size = originalCodeDTOS.size();
        if (surplusAmount < size) {
            return new Result<List<OriginalCodeDTO>>().ok(originalCodeDTOS, "当前余额" + surplusAmount + "行，余额不足，请联系管理员");
        }
        return new Result<List<OriginalCodeDTO>>().ok(originalCodeDTOS);
    }

    /**
     * 获取编码
     * @return the result
     * @author hero
     * @date 2024 -09-18 14:25:03
     */
    @PostMapping(value = "/generateLabelWithExample")
    public Result<List<CodeData>> generateLabelWithExample(@RequestBody OriginalCodeReqDTO reqDTO) {
        if (ObjectUtil.isEmpty(reqDTO)){
            return new Result<List<CodeData>>().ok(Lists.newArrayList());
        }
        if (CollectionUtils.isNotEmpty(reqDTO.getOriginalCodeList())) {
            organizationService.addUsedAmount(reqDTO.getOriginalCodeList().size());
            log.info("编码次数增加: {}", reqDTO.getOriginalCodeList().size());
        }
        return new Result<List<CodeData>>().ok(codeDataService.generateLabelWithExample(reqDTO));
    }

    /**
     * 码表分层
     * @param projectId the id
     * @return the result
     * @author hero
     * @date 2024 -09-18 14:25:03
     */
    @GetMapping(value = "/generateNetName")
    public Result<List<CodeData>> generateNetName(@RequestParam("projectId") Integer projectId, @RequestParam(value = "code", required = false) Integer code) {
        if (ObjectUtil.isEmpty(projectId)){
            return new Result<List<CodeData>>().ok(Lists.newArrayList());
        }
        return new Result<List<CodeData>>().ok(codeDataService.generateNetName(projectId, code));
    }


    /**
     * 确认码表分层
     * @return the result
     * @author hero
     * @date 2024 -09-18 14:25:03
     */
    @PostMapping(value = "/matchNetName")
    public Result<List<CodeData>> matchNetName(@RequestBody MatchNetNameReqDTO reqDTO) {
        if (ObjectUtil.isEmpty(reqDTO) || ObjectUtil.isEmpty(reqDTO.getProjectId()) || ObjectUtil.isEmpty(reqDTO.getNetList())){
            return new Result<List<CodeData>>().ok(Lists.newArrayList());
        }
        return new Result<List<CodeData>>().ok(codeDataService.matchNetName(reqDTO));
    }


    /**
     * 码表合并推荐
     * @return the result
     * @author hero
     * @date 2024 -09-18 14:25:03
     */
    @PostMapping(value = "/similarCombine")
    public Result<List<List<CodeData>>> similarCombine(@RequestBody MatchNetNameReqDTO reqDTO) {
        if (ObjectUtil.isEmpty(reqDTO) || ObjectUtil.isEmpty(reqDTO.getProjectId())){
            return new Result<List<List<CodeData>>>().ok(null);
        }
        return new Result<List<List<CodeData>>>().ok(codeDataService.similarCombine(reqDTO));
    }

    /**
     * 码表合并确认
     * @return the result
     * @author hero
     * @date 2024 -09-18 14:25:03
     */
    @PostMapping(value = "/confirmCombine")
    public Result<Boolean> similarCombine(@RequestBody ConfirmCombineDTO reqDTO) {
        if (ObjectUtil.isEmpty(reqDTO) || ObjectUtil.isEmpty(reqDTO.getProjectId())){
            return new Result<Boolean>().ok(false);
        }
        return new Result<Boolean>().ok(codeDataService.confirmCombine(reqDTO));
    }


    /**
     * 获取最新编码
     * @param projectId the id
     * @return the result
     * @author hero
     * @date 2024 -09-18 14:25:03
     */
    @GetMapping(value = "/getLastCodeByProjectId")
    public Result<Integer> getLastCodeByProjectId(@RequestParam("projectId") Integer projectId) {
        if (ObjectUtil.isEmpty(projectId)){
            return new Result<Integer>().ok(null);
        }
        return new Result<Integer>().ok(codeDataService.getLastCodeByProjectId(projectId));
    }

    /**
     * 码表合并
     * @return the result
     * @author hero
     * @date 2024 -09-18 14:25:03
     */
    @PostMapping(value = "/combineByChoose")
    public Result<Boolean> combineByChoose(@RequestBody MatchNetNameReqDTO reqDTO) {
        if (ObjectUtil.isEmpty(reqDTO) || ObjectUtil.isEmpty(reqDTO.getProjectId()) || ObjectUtil.isEmpty(reqDTO.getNetList())){
            return new Result<Boolean>().ok(false);
        }
        return new Result<Boolean>().ok(codeDataService.combineByChoose(reqDTO));
    }

    /**
     * 排序
     * @return the result
     * @author hero
     * @date 2024 -09-18 14:25:03
     */
    @PostMapping(value = "/sort")
    public Result<Boolean> sortCode(@RequestBody CodeDataSortReqDTO sortReqDTO) {
        if (ObjUtils.isEmpty(sortReqDTO) || ObjUtils.isEmpty(sortReqDTO.getModifyCode())
        || ObjUtils.isEmpty(sortReqDTO.getTargetCode()) || ObjUtils.isEmpty(sortReqDTO.getPosition())){
            return new Result<Boolean>().ok(false);
        }
        return new Result<Boolean>().ok(codeDataService.sortCode(sortReqDTO));
    }
}
